package main

import (
	"fmt"
	"log"
	"net/http"
)

func main() {
	// 将请求的URL与处理请求的函数关联
	http.HandleFunc("/hello", helloRequest)
	http.HandleFunc("/", getRequest)

	// 启动web服务器
	log.Println("Listening on http://localhost:9999/")
	log.Fatal(http.ListenAndServe(":9999", nil))
}

// /hello请求的基本处理程序
func helloRequest(w http.ResponseWriter, r *http.Request) {

	// Fprint writes to a writer, in this case the http response
	fmt.Fprint(w, "Hallo Welt")
	return
}

// 该函数只提供请求的文件，如果请求了目录，则提供索引列表
func getRequest(w http.ResponseWriter, r *http.Request) {
	file_requested := "./" + r.URL.Path
	http.ServeFile(w, r, file_requested)
	return
}
